function initOrderTicketModel(sequelize, DataTypes) {
  const OrderTicket = sequelize.define('OrderTicket', {
    orderTicketId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    orderId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'orders',
        key: 'orderId'
      }
    },
    ticketId: {
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'tickets',
        key: 'ticketId'
      }
    },
    quantity: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: 1,
      validate: {
        min: 1
      }
    },
    unitPrice: {
      type: DataTypes.FLOAT,
      allowNull: false,
      validate: {
        min: 0
      }
    }
  }, {
    tableName: 'order_tickets',
    indexes: [
      {
        name: 'idx_order_id',
        fields: ['orderId']
      },
      {
        name: 'idx_ticket_id',
        fields: ['ticketId']
      }
    ]
  });

  OrderTicket.associate = function(models) {
    OrderTicket.belongsTo(models.Order, {
      foreignKey: 'orderId',
      as: 'order'
    });
    
    OrderTicket.belongsTo(models.Ticket, {
      foreignKey: 'ticketId',
      as: 'ticket'
    });
  };

  return OrderTicket;
}

module.exports = initOrderTicketModel;